package com.ad.nbi.epirpsystem.server;

import java.util.Map;

import org.omg.CORBA.ORB;
import org.omg.CORBA.StringHolder;

import com.ad.nbi.Constants;

import EPIRPConstDefs.IRPElement;
import EPIRPConstDefs.Result;
import EPIRPConstDefs.SupportedIRP;
import EPIRPConstDefs.SupportedIRPListHolder;
import EPIRPNotifications.NotifyIRPInfoChangesOperations;
import EPIRPSystem.EPIRPOperations;
import EPIRPSystem.GetEPIRPNotificationProfile;
import EPIRPSystem.GetEPIRPOperationsProfile;
import EPIRPSystem.GetEPIRPVersions;
import EPIRPSystem.GetIRPOutline;
import EPIRPSystem.GetIRPReference;
import EPIRPSystem.InvalidIRPVersion;
import EPIRPSystem.ReleaseIRPReference;
import EPIRPSystem.UnknownIRPReference;
import ManagedGenericIRPConstDefs.Method;
import ManagedGenericIRPSystem.InvalidParameter;
import ManagedGenericIRPSystem.OperationNotSupported;

/**
 * This class is the implemetation object for your IDL interface.
 * 
 * Let the Eclipse complete operations code by choosing 'Add unimplemented
 * methods'.
 */
public class EPIRPServerImpl extends EPIRPSystem.EPIRPPOA {

	public static final String IOR_KEY_CSIRP = "FIBERHOMESubNetwork=FH,SubNetwork=oss1,ManagementNode=ONRM,IRPAgent=IRPAgentCSIRP=3CSIRP";
	public static final String IOR_KEY_FILE = "FIBERHOMESubNetwork=FH,SubNetwork=oss1,ManagementNode=ONRM,IRPAgent=IRPAgentFileTransferIRP=2FileTransferIRP";
	private ORB orb;
	private Map<String, String> iors;

	public void setORB(ORB orb_val) {
		orb = orb_val;
	}

	/**
	 * Constructor for EPIRPServerImpl
	 */
	public EPIRPServerImpl(Map<String, String> iors) {
		this.iors = iors;
	}

	@Override
	public Result get_irp_outline(String irp_version,
			SupportedIRPListHolder supported_irp_list)
			throws InvalidIRPVersion, GetIRPOutline {
		if (!Constants.VERSION.EPIRP.equals(irp_version)) {
			throw new InvalidIRPVersion();
		}
		EPIRPConstDefs.IRPManagementScopeOpt irpManagementScope = new EPIRPConstDefs.IRPManagementScopeOpt();
		irpManagementScope
				.value(new String[] {
						"SubNetwork=FH,SubNetwork=oss1,SubNetwork=EPC,ManagedElement=ASR5K,MmeFunction=MME_ManagedFunction",
						"SubNetwork=FH,SubNetwork=oss1,SubNetwork=EPC,ManagedElement=ASR5K,ServingGwFunction=SERVINGGW_ManagedFunction",
						"SubNetwork=FH,SubNetwork=oss1,SubNetwork=EPC,ManagedElement=ASR5K,PgwFunction=PDNGW_ManagedFunction" });

		SupportedIRP supportedIRP = new SupportedIRP(
				"SubNetwork=FH,SubNetwork=oss1,ManagementNode=ONRM,IRPAgent=IRPAgent",
				new EPIRPConstDefs.IRPElement[] {
						new IRPElement("EPIRP=1", "EPIRP",
								Constants.VERSION.EPIRP,
								new EPIRPConstDefs.IRPManagementScopeOpt()),
						new IRPElement("FileTransferIRP=2", "FileTransferIRP",
								Constants.VERSION.FileTransferIRP,
								new EPIRPConstDefs.IRPManagementScopeOpt()),
						new IRPElement("CSIRP=3", "CSIRP",
								Constants.VERSION.CSIRP,
								new EPIRPConstDefs.IRPManagementScopeOpt()),
						new IRPElement("NotificationIRP=4", "NotificationIRP",
								Constants.VERSION.Notification,
								new EPIRPConstDefs.IRPManagementScopeOpt()),
						new IRPElement("AlarmIRP=5", "AlarmIRP",
								Constants.VERSION.Alarm, irpManagementScope),
						new IRPElement("BasicCMIRP=6", "BasicCMIRP",
								Constants.VERSION.BasicCM, irpManagementScope),
						// new IRPElement("KernelCMIRP=7", "KernelCMIRP",
						// "KernelCM IRP V2.0.0", irpManagementScope),
						new IRPElement("BulkCMIRP=7", "BulkCMIRP",
								Constants.VERSION.BulkCM, irpManagementScope),
						new IRPElement("PMIRP=8", "PMIRP", "PM IRP V2.0.0",
								irpManagementScope),
						new IRPElement("SimpleUploadBulkCMIRP=9",
								"SimpleUploadBulkCMIRP",
								Constants.VERSION.SimpleUploadBulkCM,
								irpManagementScope),
						new IRPElement("ControlledUploadBulkCMIRP=10",
								"ControlledUploadBulkCMIRP",
								Constants.VERSION.ControlledUploadBulkCM,
								irpManagementScope)

				});

		EPIRPConstDefs.SupportedIRP[] initial = { supportedIRP };
		supported_irp_list.value = initial;
		return Result.OK;
	}

	@Override
	public Result get_irp_reference(String manager_identifier,
			String system_dn, String irp_id, String irpClassName,
			StringHolder irp_reference) throws GetIRPReference,
			InvalidParameter {
		String id = manager_identifier + system_dn + irp_id + irpClassName;

		if (IOR_KEY_FILE.equals(id)) {
			irp_reference.value = iors.get(IOR_KEY_FILE);
		} else if (IOR_KEY_CSIRP.equals(id)) {
			irp_reference.value = iors.get(IOR_KEY_CSIRP);
		} else {
			throw new InvalidParameter();
		}

		return Result.OK;
	}

	@Override
	public Result release_irp_reference(String manager_identifier,
			String irp_reference) throws UnknownIRPReference,
			ReleaseIRPReference {
		return Result.OK;
	}

	@Override
	public String[] get_ep_irp_versions() throws GetEPIRPVersions {

		String[] version = { Constants.VERSION.EPIRP };
		return version;
	}

	@Override
	public Method[] get_ep_irp_operations_profile(String irp_version)
			throws InvalidParameter, OperationNotSupported,
			GetEPIRPOperationsProfile {
		if (!Constants.VERSION.EPIRP.equals(irp_version)) {
			throw new InvalidParameter();
		}
		Method[] methods = new Method[] { new Method("get_alarm_irp_versions",
				new String[] { "String:version" })

		};

		return methods;
	}

	@Override
	public Method[] get_ep_irp_notification_profile(String irp_version)
			throws InvalidParameter, OperationNotSupported,
			GetEPIRPNotificationProfile {
		if (!Constants.VERSION.EPIRP.equals(irp_version)) {
			throw new InvalidParameter();
		}
		Method[] methods = new Method[] { new Method(
				NotifyIRPInfoChangesOperations.ET_IRPINFO_CHANGES,
				new String[] { "d", "e", "a", "b", "c", "IRP_DN",
						"CHANGE_MODE", "ADDITIONAL_TEXT" }) };
		return methods;
	}
}
